﻿using System.Collections.Generic;

namespace TC
{
    public class SwitchInt : FlowControlNode
    {
        public ValueInput<int> selection;
        public Dictionary<int, FlowOutput<FlowNode>> cases;
        public FlowOutput<FlowNode> defaultCase;
        public FlowInput<FlowNode> flowIn;

        public override void OnCreate()
        {
            base.OnCreate();
            
            cases = new Dictionary<int, FlowOutput<FlowNode>>();
            selection = this.AddValueInPort<int>("selection");
            defaultCase = this.AddFlowOutPort<FlowNode>("default");
            flowIn = this.AddFlowInPort<FlowNode>("In");
        }

        public override void Execute(Dispatcher chain)
        {
            base.Execute(chain);

            int i = selection.Value;
            if (cases.TryGetValue(i, out var item))
            {
                chain.Call(item);
            }
            else
            {
                chain.Call(defaultCase);
            }
        }
    }
}
